package com.piece.admin.service;

import com.piece.admin.model.TbSysPost;
import com.piece.admin.repository.TbSysPostRepository;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.enums.StatusType;
import com.piece.core.framework.enums.sql.SqlKey;
import com.piece.core.framework.exception.ErrorCode;
import com.piece.core.framework.exception.FrameWorkException;
import com.piece.core.framework.util.collection.CollectionUtil;
import com.piece.core.framework.util.collection.MapUtil;
import com.piece.core.framework.util.object.ObjectUtil;
import com.piece.core.jpa.repository.BaseService;
import com.piece.core.framework.support.bootstrap.Order;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.Select;
import com.piece.core.framework.util.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.*;

@Service
@Transactional
public class TbSysPostService extends BaseService<TbSysPost, String> {

    @Autowired
    private TbSysPostRepository tbSysPostRepository;

    @PostConstruct
    public void initRepository() {
        setRepository(tbSysPostRepository);
    }

    /**
     * 批量删除用户信息
     */
    @Override
    public void delete(Serializable id) {
        tbSysPostRepository.updateByProperties("Q_postId_L_EQ", id, "status", StatusType.DELETE.getCode());
    }

    /**
     * 批量删除用户信息
     */
    @Override
    public int delete(Serializable[] ids) {
        List<Long> param_in = Arrays.asList((Long[]) ids);
        return tbSysPostRepository.updateByProperties("Q_postId_L_IN", param_in, "status", StatusType.DELETE.getCode());
    }

    /**
     * 初始化实体类中所对应的岗位select
     */
    public List<Select.Entity> initSelect(String selected) {
        List<Select.Entity> result = new ArrayList<>();
        if (StringUtil.isNotEmpty(selected)) {
            HashMap<String, Object> condition = new HashMap<>();
            condition.put("Q_postId_L_IN", Arrays.asList(Convert.toLongArray(selected)));
            condition.put("status", StatusType.NORMAL.getCode());
            List<TbSysPost> tbSysPostList = tbSysPostRepository.findByCondition(condition, generateDefaultOrder());
            if (CollectionUtil.isNotEmpty(tbSysPostList)) {
                Select select = Select.builder().build();
                Select.Entity temp;
                for (TbSysPost post : tbSysPostList) {
                    temp = select.new Entity(Convert.toStr(post.getPostId()), post.getPostName());
                    result.add(temp);
                }
                select = null;
            }
        }
        return result;
    }

    public void validPost(Collection<Long> ids) {
        if (ObjectUtil.isEmpty(ids)) {
            return;
        }

        List<TbSysPost> posts = tbSysPostRepository.findByCondition(MapUtil.builder(new HashMap<String, Object>())
                .put("postId", new ArrayList<>(ids))
                .put("status", StatusType.NORMAL.getCode())
                .build(), null);
        Map<Long, TbSysPost> postMap = CollectionUtil.convertMap(posts, TbSysPost::getPostId);
        ids.forEach(id -> {
            TbSysPost post = postMap.get(id);
            if (null == post) {
                throw new FrameWorkException(new ErrorCode(ExceptionConstants.NOT_EXIST, "岗位【" + id + "】"));
            }

            if (!StatusType.NORMAL.getCode().equals(post.getStatus())) {
                throw new FrameWorkException(new ErrorCode(ExceptionConstants.IS_DISABLE, post.getPostName()));
            }
        });
    }

    @Override
    protected List<Order> generateDefaultOrder() {
        List<Order> orders = new ArrayList<>();
        orders.add(new Order("createTime", SqlKey.DESC));
        return orders;
    }
}
